package cn.sh.library.gmwx.utils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 14-3-10.
 */
public class Formatter {
    //标准化日期格式
    public static String convert2SD(String date) {
        if (date.contains(".")) {
            date.replaceAll(".","-");
        }

        String[] cells = date.split("-");
        if (1 == cells.length) {
            return date + "-01-01";
        }

        if (2 == cells.length) {
            return date + "-01";
        }

        return date;
    }

    //标准化字符串，去除空格
    public static String trimNullStr(String str) {
        if (str.contains(" ")) {
            return str.replaceAll(" ", "-");
        }

        return str;
    }

    public static Map list2map(List<Map<String, String>> mapList, String tag) {
        Map map = new LinkedHashMap();

        for (int i=0;i<mapList.size();i++) {
            String k = ((Map) mapList.get(i)).get(tag).toString();
            int v = i + 1;
            map.put(k, v);
        }

        return map;
    }

    public static Map list2mapIv(List<Map<String, String>> mapList, String tag) {
        Map<Object, Object> map = new LinkedHashMap();

        for (int i=0;i<mapList.size();i++) {
            String v = RDFUtils.getValue(((Map) mapList.get(i)).get(tag).toString());
            int k = i + 1;
            map.put(k, Integer.parseInt(v));
        }

        return map;
    }

    //过滤特殊字符
    /**
     * “javascript”、“<script>”、“#”、“&”、<iframe>、“'”、">"、"<"、“%”、"\"\""、","、"."、">="、"=<"、"<>"、"-"、"_"、";"、"||"、"["、"]"、"/"、"|"
     */
//    public static String stringFilter(String str) {
//        if (StringUtils.isNotBlank(str.replaceAll("\\+", ""))) {
//            String regEx = "[`~'\"<>\\[\\]/\\\\]";
//            Pattern p = Pattern.compile(regEx);
//            Matcher m = p.matcher(str);
//            return m.replaceAll("").trim();
//        }
//
//        return "";
//    }
}
